package com.ruoyi.media.vipProduct.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.media.vipProduct.domain.bo.MediaVipProductBo;
import com.ruoyi.media.vipProduct.domain.vo.MediaVipProductVo;
import com.ruoyi.media.vipProduct.domain.MediaVipProduct;
import com.ruoyi.media.vipProduct.mapper.MediaVipProductMapper;
import com.ruoyi.media.vipProduct.service.IMediaVipProductService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 会员套餐管理Service业务层处理
 *
 * @author dqw
 * @date 2023-05-24
 */
@RequiredArgsConstructor
@Service
public class MediaVipProductServiceImpl extends ServiceImpl<MediaVipProductMapper,MediaVipProduct> implements IMediaVipProductService {

    private final MediaVipProductMapper baseMapper;

    /**
     * 查询会员套餐管理
     */
    @Override
    public MediaVipProductVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询会员套餐管理列表
     */
    @Override
    public TableDataInfo<MediaVipProductVo> queryPageList(MediaVipProductBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MediaVipProduct> lqw = buildQueryWrapper(bo);
        Page<MediaVipProductVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询会员套餐管理列表
     */
    @Override
    public List<MediaVipProductVo> queryList(MediaVipProductBo bo) {
        LambdaQueryWrapper<MediaVipProduct> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MediaVipProduct> buildQueryWrapper(MediaVipProductBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MediaVipProduct> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeptId() != null, MediaVipProduct::getDeptId, bo.getDeptId());
        lqw.eq(bo.getUserId() != null, MediaVipProduct::getUserId, bo.getUserId());
        lqw.eq(bo.getVipType() != null, MediaVipProduct::getVipType, bo.getVipType());
        lqw.like(StringUtils.isNotBlank(bo.getName()), MediaVipProduct::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), MediaVipProduct::getDescription, bo.getDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getSoftwareBasics()), MediaVipProduct::getSoftwareBasics, bo.getSoftwareBasics());
        lqw.eq(StringUtils.isNotBlank(bo.getPrice()), MediaVipProduct::getPrice, bo.getPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getBackgroudImage()), MediaVipProduct::getBackgroudImage, bo.getBackgroudImage());
        lqw.eq(StringUtils.isNotBlank(bo.getButtonColor()), MediaVipProduct::getButtonColor, bo.getButtonColor());
        return lqw;
    }

    /**
     * 新增会员套餐管理
     */
    @Override
    public Boolean insertByBo(MediaVipProductBo bo) {
        MediaVipProduct add = BeanUtil.toBean(bo, MediaVipProduct.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改会员套餐管理
     */
    @Override
    public Boolean updateByBo(MediaVipProductBo bo) {
        MediaVipProduct update = BeanUtil.toBean(bo, MediaVipProduct.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MediaVipProduct entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除会员套餐管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
